package com.shfl.task.common;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ResCodeMessage implements InitializingBean {

    private static final String Success = "0000";

    private Resource[] codeXml;

    public void setCodeXml(Resource[] codeXml) {
        this.codeXml = codeXml;
    }

    public static Map<String, String> ResMessage = new HashMap<>();

    public static String getMessageDesc(String code) {
        return ResMessage.get(code);
    }

    public static final String getSuccess() {
        return Success;
    }

    public static Map<String, String> getResMessage() {
        return ResMessage;
    }

    public static void setResMessage(Map<String, String> var0) {
        ResMessage = var0;
    }

    @Override
    public void afterPropertiesSet() throws Exception {

        for (Resource resource : codeXml) {
            SAXReader reader = new SAXReader();
            Document document = reader.read(resource.getInputStream());
            Element root = document.getRootElement();
            List<Element> list = root.elements();
            for (Element e : list) {
                ResMessage.put(e.attribute("code").getValue(), e.attribute("desc").getValue());
            }
        }
    }
}
